package com.fancy.learncenter.net;

import android.util.Log;

import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.utils.LogUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Hyy on 2016/9/29.
 */
public class HttpMehtod {
    private static final int DEFAULT_TIMEOUT = 5;

    private Retrofit retrofit;
    private static HttpMehtod instance;

    //构造方法私有
    private HttpMehtod() {
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder builder = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor());
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

        retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())

                .baseUrl(Constant.BASE_URL)

                .build();
    }

    //获取单例
    public static HttpMehtod getInstance() {
        if (instance == null) {
            instance = new HttpMehtod();
        }
        return instance;
    }

    public void testRetrofit() {
        ApiService testService = retrofit.create(ApiService.class);
        testService
                .testRetrofit("sww", "123")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomSubscriber<Response<ResponseBody>>() {

                               @Override
                               public void onError(Throwable e) {
                               }

                               @Override
                               public void onNext(Response<ResponseBody> stringResponse) {
                                   try {

                                       LogUtil.MyLog("sss", "sss");

                                   } catch (Exception e) {
                                       e.printStackTrace();
                                   }
                               }
                           }
                );
    }


    /**
     * 请求拦截
     */
    public static class LoggingInterceptor implements Interceptor {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
//            long t1 = System.nanoTime();

            okhttp3.Response response = chain.proceed(request);
            LogUtil.MyLog("net_requset", response.toString());
//            long t2 = System.nanoTime();

            return response;
        }
    }

    public void test(Subscriber subscriber) {
        ApiService testService = retrofit.create(ApiService.class);
        testService
                .test("测试")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

}
